package com.itheima.controller.upload;

import com.itheima.entity.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @Name ImgUploadController
 * @Author BaiXin
 * @Date 2022-10-22 11:46
 */
@RestController
@RequestMapping("/game")
public class ImgUploadController {

    @RequestMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile multipartFile) throws IOException {
        String fileName = multipartFile.getOriginalFilename();

        String suffixName = fileName.substring(fileName.lastIndexOf("."));

        String newName = UUID.randomUUID().toString().replaceAll("-", "");

        multipartFile.transferTo(new File
                ("E:\\BaiduNetdiskDownload\\" +
                        "第3阶段—Java开发框架\\3.2、2022版SSM框架\\作业提交\\" +
                        "20221021_白鑫\\springmvc_task_02\\src\\main\\webapp\\images\\"+
                        newName+suffixName));
        System.out.println(newName+suffixName);
        return Result.success(20051,"文件上传成功","/images/"+newName+suffixName);

    }
}
